package com.bg.sparkproject.math;

/**
 * @Auther: zhengshunzhi
 * @Date: 2018/12/17 10:14
 * @Description:
 */
public class MathUtil {
    public static void main(String[] args) {
        System.out.println(bigNumberSum("361", "25"));
        System.out.println(bigNumberMultiply2("55", "555"));
    }

    /**
     * 大整数求和
     *
     * @param bigNumberA 大整数A
     * @param bigNumberB 大整数B
     */
    public static String bigNumberSum(String bigNumberA, String bigNumberB) {
        //1.把两个大整数用数组逆序存储，数组长度等于较大整数位数+1
        int maxLength = bigNumberA.length() > bigNumberB.length() ? bigNumberA.length() : bigNumberB.length();
        int[] arrayA = new int[maxLength + 1];

        for (int i = 0; i < bigNumberA.length(); i++) {
            arrayA[i] = bigNumberA.charAt(bigNumberA.length() - 1 - i) - '0';
        }

        int[] arrayB = new int[maxLength + 1];

        for (int i = 0; i < bigNumberB.length(); i++) {
            arrayB[i] = bigNumberB.charAt(bigNumberB.length() - 1 - i) - '0';
        }

        //2.构建result数组，数组长度等于较大整数位数+1
        int[] result = new int[maxLength + 1];

        //3.遍历数组，按位相加
        for (int i = 0; i < result.length; i++) {
            int temp = result[i];
            temp += arrayA[i];
            temp += arrayB[i];

            //判断是否进位
            if (temp >= 10) {
                temp = temp - 10;
                result[i + 1] = 1;
            }
            result[i] = temp;

        }

        //4.把result数组再次逆序并转成String
        StringBuilder sb = new StringBuilder();
        //是否找到大整数的最高有效位
        boolean findFirst = false;
        for (int i = result.length - 1; i >= 0; i--) {
            if (!findFirst) {
                if (result[i] == 0) {
                    continue;
                }
                findFirst = true;
            }
            sb.append(result[i]);

        }

        return sb.toString();
    }

    /**
     * 大数相乘方法二
     */
    public static String bigNumberMultiply2(String bigNumA, String bigNumB) {
        int[] num1 = new int[bigNumA.length()];
        int[] num2 = new int[bigNumB.length()];

        for (int i = 0; i < bigNumA.length(); i++) {
            num1[i] = bigNumA.charAt(i) - '0';
        }

        for (int i = 0; i < bigNumB.length(); i++) {
            num2[i] = bigNumB.charAt(i) - '0';
        }

        // 分配一个空间，用来存储运算的结果，num1长的数 * num2长的数，结果不会超过num1+num2长
        int[] result = new int[num1.length + num2.length];

        // 先不考虑进位问题，根据竖式的乘法运算，num1的第i位与num2的第j位相乘，结果应该存放在结果的第i+j位上
        for (int i = 0; i < num1.length; i++) {
            for (int j = 0; j < num2.length; j++) {
                result[i + j + 1] += num1[i] * num2[j];  // (因为进位的问题，最终放置到第i+j+1位)
            }
        }

        //单独处理进位
        for (int k = result.length - 1; k > 0; k--) {
            if (result[k] > 10) {
                result[k - 1] += result[k] / 10;
                result[k] %= 10;
            }
        }

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < result.length; i++) {
            if (i == 0 && result[i] == 0) continue;
            builder.append(result[i]);
        }
        return builder.toString();
    }
}
